// Learn TypeScript:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/typescript.html
// Learn Attribute:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/reference/attributes.html
// Learn life-cycle callbacks:
//  - https://docs.cocos.com/creator/2.4/manual/en/scripting/life-cycle-callbacks.html

import AnimalManager from "../Manager/AnimalManager";
import GeZiManager from "../Manager/GeZiManager";
import GMessage from "../Message/GMessage";
import KHD from "../Message/KHD";
import KHD2 from "../Message/KHD2";
import { Message } from "../Message/Message";
import { SHMessage } from "../Message/SHMessage";


import turn from "../game/time/turn";
import Character from "./Character";
import Cspine from "./spineANDgif/Cspine";
import { gif } from "./spineANDgif/gif";





const {ccclass, property} = cc._decorator;
@ccclass
export default class ComponentBase extends cc.Component {
   Behurt222(k:number, arg1: this, arg2: any) {
   
   }
 ZB:number=0
 FW:number[]=[]
  ObstaclesName: string;
  color: string;
  tt: string;
 BZ(preZB:number){}
 LK(ZB:number){}

 JR(ZB:number){}
  WD1(){}
  
  T0(turn: number) {
 
   }
   mg(m:string) {
      //throw new Error("Method not implemented.");
   }
   Kill2() {
    
   }
  Attack1() {

  }
   Qi3() {
     
   }
    Magic3(ZB:number) {
      
    }
  dayPass() {
  
  }
 
   HPM3(){}

FH1(n:number){}
   
   HPM2(n:number) {
   }
   Walk25(ZB: number) {
      
   }
   Behurt3(n: number,killp:any,k:number,BK:number) {
   }
    Turn3(N?:number) {
      
    }
    Turn1(N?:number) {
        
    }
   Dead2(dead:number) {
      
   }
   Dead1() {
     
   }
    T1(turn:number) {
        
    }
   Walk1() {
   
   }
    Magic2() {
        
    }
    
    Magic1() {}
  Move2(arg0: number,Pturn:number,time:number) {}
  Move3(ZB: number,pturn: number) {}
  Attack3(an: number[]) {}
  Attack35(an:any,kn:number) {}
   use:boolean=true;
  P6F() { 
}
    T3() {}
   Behurt2(m:SHMessage,BeHC:any,HC:any) {}
   Walk3(ZB: number) {}
    Shop3() {}
    Shop1() {}
    Shop2(A:string,s:boolean) { }
  Walk2(ZB:number) {}
  Attack2(an:number[]){} 
  Attack25(at:any,csh:number){} 
    WDT() {
    }  
   ReceiveMessage(message:Message){}
  start(){
}
Kill3(n:number){}
turnPass(){}
xd1(message:GMessage){}
xd2(message:GMessage){}
findGe(face: number,i:number,ZB:number) {
  let n = 0;
  let a = ZB;
let x:number;

let y:number;
if(ZB % 8 == 0) {x = 8 } else {x = ZB % 8; }
y = Math.ceil(ZB / 8)

  for (let b = 0; b < i; b++) {
    if ((x === 1) && (face === 4)) { n = 66 } else if ((x === 8) && (face === 6)) { n = 66; } else if ((y === 1) && (face === 2)) { n = 66; } else if ((y === 8) && (face === 8)) { n = 66; }
    else {

      switch (face) {
        case 1: n = a - 9
          break;

        case 2: n = a - 8
          break;

        case 3: n = a - 7
          break;
        case 4: n = a - 1
          break;
        case 5: n = a
          break;
        case 6: n = a + 1
          break;
        case 7: n = a + 7
          break;
        case 8: n = a + 8
          break;
        case 9: n = a + 9
          break;
      }
      a = n;
    }
  }
  return n;
}
getNIG(n:number){
let NK:number[]=[];
  for (let index = 1; index <= 9; index++) {
    
   NK.push(this.findGe(index,1,n))
  }
  return NK;
}


getNIGno5(n:number){
  let NK:number[]=[];
    for (let index = 1; index <= 9; index++) {
   if (index!=5) {
    NK.push(this.findGe(index,1,n))
   }   
    

    }
    return NK;
  }



roundPass(){
}




Qi1(n:number){}
Qi2(n:number,pn:number,m:SHMessage){}

PPC(){
  if(KHD2.PT.includes(turn.turn)){
    return true
  }else{return false}
}


  
turn8(arr: number[]): number[] {
  const transformationMap = {
    1: 9,
    2: 8,
    3: 7,
    4: 6,
    5: 5,
    6: 4,
    7: 3,
    8: 2,
    9: 1
  };
  for (let i = 0; i < arr.length; i++) {
    arr[i] = transformationMap[arr[i]] || arr[i];
  }
  return arr;
}//通用校对的基本方法，不用理会
  
tt6(arr:number): number {
  const transformationMap = {
    1: 3,
    2: 6,
    3: 7,
    4: 2,
    5: 5,
    6: 8,
    7: 1,
    8: 4,
    9: 7
  };

    arr = transformationMap[arr]
 
  return arr;
}//通用校对的基本方法，不用理会
  
tt4(arr:number): number {
  const transformationMap = {
    3: 1,
    6: 2,
   7: 3,
    2: 4,
    5: 5,
    8: 4,
    1: 7,
   4: 8,
    9:3
  };
  arr = transformationMap[arr]
 
  return arr;
}
  
 d2468(a: number, b: number): number {
  if (a === b) {
    return 2; // 同
  } else if (a === 2) {
    if (b === 4) {
      return 6; // 顺时针转90度
    } else if (b === 6) {
      return 8; // 180度
    } else if (b === 8) {
      return 4; // 逆时针转90度
    }
  } else if (a === 4) {
    if (b === 2) {
      return 8; // 180度
    } else if (b === 6) {
      return 2; // 同
    } else if (b === 8) {
      return 6; // 顺时针转90度
    }
  } else if (a === 6) {
    if (b === 2) {
      return 4; // 逆时针转90度
    } else if (b === 4) {
      return 8; // 180度
    } else if (b === 8) {
      return 2; // 同
    }
  } else if (a === 8) {
    if (b === 2) {
      return 6; // 顺时针转90度
    } else if (b === 4) {
      return 2; // 同
    } else if (b === 6) {
      return 4; // 逆时针转90度
    }
  }
  return -1; // 非法输入，返回-1表示无效结果
}
  
fadeOutAndDisappear(node:cc.Node, duration:number) {
  const fps = 60;
  const totalFrames = Math.ceil(duration / 1000 * fps);
  const step = node.opacity / totalFrames;
  let currentFrame = 0;
  
  const interval = setInterval(() => {
      node.opacity -= step;
      currentFrame++;
      
      if (currentFrame === totalFrames) {
          clearInterval(interval);
          node.active = false; // 隐藏节点
      }
  }, 1000 / fps);
}


fadeINAndA(node:cc.Node, duration:number) {
  node.active=true;
  node.opacity=0;
  const fps = 60;
const totalFrames = Math.ceil(duration / 1000 * fps);
const step = 255 / totalFrames;
let currentFrame = 0;

const interval = setInterval(() => {
    node.opacity += step;
    currentFrame++;
    
  
}, 1000 / fps);
}

fadeINAndAK(node:cc.Node, duration:number,n:number) {
  node.active=true;
  node.opacity=0;
  const fps = 60;
const totalFrames = Math.ceil(duration / 1000 * fps);
const step = n / totalFrames;
let currentFrame = 0;

const interval = setInterval(() => {
    node.opacity += step;
    currentFrame++;
    
  
}, 1000 / fps);
}

NtoColor(n){
if(n<0){return "Green"}
  switch (n%2) {
  case 1:return "Blue"
  case 0:return "Red"
   

  default:
    break;
}

}

ColortoN(c:String){

switch (c) {
  case "Blue":return 1
  case "Red":return 0
  case "Green":return -1

  default:
    break;
}

}

getpos(ZB:number){




    let point;
    switch (ZB) {
      case 0: point = new cc.Vec2(0, 0);

        break;


      case 1: point = new cc.Vec2(300, 65);
        break;
      case 2: point = new cc.Vec2(634, 65);
        break;
      case 3: point = new cc.Vec2(906, 65);
        break;
      case 4: point = new cc.Vec2(1188, 65);
        break;
      case 5: point = new cc.Vec2(1485, 65);
        break;
      case 6: point = new cc.Vec2(1774, 65);
        break;
      case 7: point = new cc.Vec2(2058, 65);
        break;
      case 8: point = new cc.Vec2(2228, 65);
        break;



      case 9: point = new cc.Vec2(445, 270);
        break;
      case 10: point = new cc.Vec2(684, 270); console.log("10");
        break;
      case 11: point = new cc.Vec2(950, 270);
        break;
      case 12: point = new cc.Vec2(1212, 270);
        break;
      case 13: point = new cc.Vec2(1465, 270);
        break;
      case 14: point = new cc.Vec2(1724, 270);
        break;
      case 15: point = new cc.Vec2(1983, 270);
        break;
      case 16: point = new cc.Vec2(2203, 270);
        break;


      case 17: point = new cc.Vec2(445, 436);
        break;
      case 18: point = new cc.Vec2(684, 436);
        break;
      case 19: point = new cc.Vec2(950, 436);
        break;
      case 20: point = new cc.Vec2(1212, 436);
        break;
      case 21: point = new cc.Vec2(1465, 436);
        break;
      case 22: point = new cc.Vec2(1724, 436);
        break;
      case 23: point = new cc.Vec2(1983, 436);
        break;
      case 24: point = new cc.Vec2(2203, 436);
        break;



      case 25: point = new cc.Vec2(572, 592);
        break;
      case 26: point = new cc.Vec2(792, 592);
        break;
      case 27: point = new cc.Vec2(1008, 592);
        break;
      case 28: point = new cc.Vec2(1229, 592);
        break;
      case 29: point = new cc.Vec2(1439, 592);
        break;
      case 30: point = new cc.Vec2(1660, 592);
        break;
      case 31: point = new cc.Vec2(1886, 592);
        break;
      case 32: point = new cc.Vec2(2083, 592);
        break;






      case 33: point = new cc.Vec2(654, 740);
        break;
      case 34: point = new cc.Vec2(843, 740);
        break;
      case 35: point = new cc.Vec2(1039, 740);
        break;
      case 36: point = new cc.Vec2(1241, 740);
        break;
      case 37: point = new cc.Vec2(1431, 740);
        break;
      case 38: point = new cc.Vec2(1630, 740);
        break;
      case 39: point = new cc.Vec2(1831, 740);
        break;
      case 40: point = new cc.Vec2(2018, 740);
        break;





      case 41: point = new cc.Vec2(719, 883);
        break;
      case 42: point = new cc.Vec2(888, 883);
        break;
      case 43: point = new cc.Vec2(1067, 883);
        break;
      case 44: point = new cc.Vec2(1246, 883);
        break;
      case 45: point = new cc.Vec2(1423, 883);
        break;
      case 46: point = new cc.Vec2(1600, 883);
        break;
      case 47: point = new cc.Vec2(1784, 883);
        break;
      case 48: point = new cc.Vec2(1958, 883);
        break;




      case 49: point = new cc.Vec2(765, 995);
        break;
      case 50: point = new cc.Vec2(924, 995);
        break;
      case 51: point = new cc.Vec2(1088, 995);
        break;
      case 52: point = new cc.Vec2(1252, 995);
        break;
      case 53: point = new cc.Vec2(1412, 995);
        break;
      case 54: point = new cc.Vec2(1578, 995);
        break;
      case 55: point = new cc.Vec2(1742, 995);
        break;
      case 56: point = new cc.Vec2(1900, 995);
        break;



      case 57: point = new cc.Vec2(815, 1080);
        break;
      case 58: point = new cc.Vec2(956, 1096);
        break;
      case 59: point = new cc.Vec2(1107, 1096);
        break;
      case 60: point = new cc.Vec2(1260, 1096);
        break;
      case 61: point = new cc.Vec2(1410, 1096);
        break;
      case 62: point = new cc.Vec2(1562, 1096);
        break;
      case 63: point = new cc.Vec2(1708, 1096);
        break;
      case 64: point = new cc.Vec2(1848, 1096);
        break;
      case 65:
        break;

    }
return point
}




mArr1Arr2<T>(...arrays: T[][]): T[] {
  const mergedArray: T[] = [].concat(...arrays);
  return mergedArray;
}
//合并相同元素？
addZ(banX: number[], useR: number[], banT: number[]): number {
  function getR(banR:number[]):number[] {
    let k=[]
      for(let a of banR){
     
      switch (a) {
          case 1:k.push(28,29,36,37);
              
              break;
              case 2:k.push(19,20,21,22,30,38,46,45,44,43,35,27)
              
              break;
              case 3:k.push(10,11,12,13,14,15,23,31,39,47,55,54,53,52,51,50,42,34,26,18)
              
              break;
      
          default:
              break;
      }
    }
    console.log(k)
      return k
  }


  function isWaterCell(x: number, y: number): boolean {
    return x === 1 || y === 1 || x === 8 || y === 8;
  }
  
 
  function getCoordinates(cellNumber: number): [number, number] {
    const row = Math.ceil(cellNumber / 8);
    const col = cellNumber % 8 === 0 ? 8 : cellNumber % 8;
    return [ col,row];
  }
  const validCoordinates: number[] = [];
  for (let i = 1; i <= 64; i++) {
    const [x, y] = getCoordinates(i);
    const isXForbidden = banX.includes(x);
    const isRingForbidden = getR(useR).includes(i)
    const isCellForbidden = banT.includes(i);
    const isWater = isWaterCell(x, y);
    
    
    if (!isXForbidden && isRingForbidden && !isCellForbidden && !isWater) {
      validCoordinates.push(i);
    }
  }

if (validCoordinates.length!=0) {
return  validCoordinates[Math.floor(Math.random() * validCoordinates.length)]
}else{return -1}
 
}



faceSide(n:number)
{

switch (n) {
  case 2:return[2,1,3]
    
    break;
    case 4:return[4,1,7]
    
    break;
    case 6:return[6,8,3]
    
    break;
    case 8:return[7,8,9]
    
    break;
  default:return[0]
    break;
}




}



getRAA<T>(arr: T[], num: number): T[] {
  let shuffled = arr.slice(0);
  for (let i = shuffled.length - 1; i > 0; i--) {
      const j = Math.floor(Math.random() * (i + 1));
      [shuffled[i], shuffled[j]] = [shuffled[j], shuffled[i]];
  }
  return shuffled.slice(0, num);
}

   getRA(arr:string[], n:number) {
  const len = arr.length;
  const result = [];
  
  if (n >= len) {
      return arr.slice(); // 如果要选取的元素数量大于等于数组长度，直接返回整个数组的副本
  }
  
  const pickedIndexes = new Set();
  while (pickedIndexes.size < n) {
      let randomIndex = Math.floor(Math.random() * len);
      pickedIndexes.add(randomIndex);
  }
  
  pickedIndexes.forEach((index: number) => {
      result.push(arr[index]);
  });
  
  return result;
}


 getWorldPos(node: cc.Node): cc.Vec3 {
    return node.convertToWorldSpaceAR(cc.Vec3.ZERO);
}


// 获取节点的世界坐标
 setWorldPos(node: cc.Node, worldPosition: cc.Vec3) {
node.position = node.parent.convertToNodeSpaceAR(worldPosition);
}


 shakeNodehh(node: cc.Node, duration: number, range: number) {
  const startPosition = node.position;
  const startScaleX = node.scaleX
  const startScaleY = node.scaleY
  cc.tween(node)
      .to(duration / 4, { position: cc.v3(startPosition.x + range, startPosition.y+range) }, { easing: "sineInOut" })
      .to(duration / 4, { position: startPosition }, { easing: "sineInOut" })
      //.to(duration / 4, { scale: startScaleX*1.1 }, { easing: "sineInOut" })
      //.to(duration / 4, { scale:  startScaleX }, { easing: "sineInOut" })
      .to(duration / 2, { opacity:  255}, { easing: "sineInOut" })
      .start();
}

// 显示子节点并抖动
 showAndShakeChildren(node: cc.Node) {
  node.children.forEach(child => {
      // 首先确保子节点是隐藏的
      child.active = false;

      // 延迟一段时间显示子节点
      cc.tween(child)
          .delay(0.5)
          .to(0.5, { opacity: 255 }, { easing: "sineInOut" })
          .call(() => {
              // 子节点显示后开始抖动
             this. shakeNodehh(child, 1, 10); // 抖动1秒，范围10
          })
          .start();
  });
}
showAndShakeChildren2(node: cc.Node,time:number,f:number) {
  node.children.forEach(child => {
      // 首先确保子节点是隐藏的
     // child.active = false;
     child.opacity=0
      // 延迟一段时间显示子节点
      cc.tween(child)
     
          .to(0.2, { opacity: 255 }, { easing: "sineInOut" })
          .call(() => {
              // 子节点显示后开始抖动
           ; // 抖动1秒，范围10
          })
          .start();
          this. shakeNodehh(child, time, f)
  });
}
// 使用方法
// 假设你有一个节点叫parentNode，它包含了你想要动画的子节点

// 使用方法
// 假设你有一个节点叫parentNode，它包含了你想要动画的子节点







   
shakeCameraAndNode(camera: cc.Camera, node: cc.Node, intensity: number, duration: number) {
  console.log("fffff")
  
  
  
     const originalCameraPos = camera.node.position.clone();
     const originalNodePos = node.position.clone();
     
     let elapsed = 0;
     const fps = 60;
     
     const interval = setInterval(() => {
         const randomX = (Math.random() - 0.5) * intensity;
         const randomY = (Math.random() - 0.5) * intensity;
         
         camera.node.setPosition(originalCameraPos.x + randomX, originalCameraPos.y + randomY);
         node.setPosition(originalNodePos.x + randomX, originalNodePos.y + randomY);
         
         elapsed += 1000 / fps;
         
         if (elapsed >= duration) {
             clearInterval(interval);
             camera.node.setPosition(originalCameraPos);
             node.setPosition(originalNodePos);
         }
     }, 1000 / fps);
 }

 fadeOut( node:cc.Node,duration05: number, targetOpacity: number): void {
  //let startOpacity = node.color.a;
  //let deltaOpacity = startOpacity - targetOpacity;

  cc.tween(node)
  .to(duration05, { opacity: targetOpacity })
  .start()


}



}